﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _110607
{
    public partial class Form1 : Form
    {
        private SqlConnection conn;
        private bool flag;
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }  

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(textBox1.Text);
                conn.Open();
                Application.DoEvents();
                label1.Text = "DB CONNECT SUCCESS.";

            }
            catch {
                label1.Text = "DB CONNECT ERR.";
                return;
            }
            string ids = "";
            SqlDataAdapter sda = new SqlDataAdapter("select id,f_CWRQ from foa_wj where f_CWRQ is not null", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            SqlCommand cmd = new SqlCommand();
            for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
            {
                if (flag) break;
                string sql = "select cast(f_CWRQ as datetime) from foa_wj where id = " + ds.Tables[0].Rows[i]["id"];
                cmd.CommandText = sql;
                cmd.Connection = conn;
                try
                {
                    object obj = cmd.ExecuteScalar();
                    label1.Text = ((float)i / (float)ds.Tables[0].Rows.Count*100).ToString()+"%";
                    Application.DoEvents();

                }
                catch (SqlException ex){
                    ids += ds.Tables[0].Rows[i]["id"]+",";
                    richTextBox1.AppendText(ds.Tables[0].Rows[i]["id"]+"\t"+ex.Message + "\t"+ds.Tables[0].Rows[i]["f_CWRQ"]+"\n");
                }
            }
            richTextBox1.AppendText(ids);
            richTextBox1.AppendText("Total:"+ids.Split(',').Length);
            label1.Text = "100%";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            flag = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2(conn);
            f.ShowDialog();
        }
    }
}
